<template>
  <div class="login">
    <div class="login-box">
        <!-- 头像区域 -->
        <div class="avatar-box">
            <img src="../assets/logo.png" alt="">
        </div>
        <!-- 表单区域 -->
        <el-form ref="form" class="login-form" :model="form" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input  v-model="form.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <!-- 密码框 -->
        <el-form-item prop="password">
        <el-input v-model="form.password" prefix-icon="iconfont icon-3702mima" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns" >
        <!--登录按钮  -->
        <el-button @click="login" type="primary" >登录</el-button>
        <!-- 重置按钮 -->
        <el-button @click= "resetForm" type="info" >重置</el-button>
        </el-form-item>
        </el-form>
    </div>
</div>
</template>

<script>
export default {
  data () {
    return {
      form: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  created () {},

  methods: {
    resetForm () {
      this.$refs.form.resetFields()
    },
    login () {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$axios.post('login', this.form)
        // console.log(res)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        // console.log(res)
        this.$message.success('登陆成功')
        sessionStorage.setItem('token', res.data.token)
        // console.dir(sessionStorage)
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style scoped lang='less'>
.login {
  width: 100%;
  height: 100%;
  background: #2b4b6b;
  .login-box {
    width: 450px;
    height: 300px;
    background: #fff;
    border-radius: 5px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avatar-box {
      width: 130px;
      height: 130px;
      border-radius: 50%;
      border: 1px solid #eee;
      padding: 10px;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background: #ffffff;
      box-shadow: 0px 0px 10px skyblue;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login-form{
        position: absolute;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
        bottom: 0;
        .btns{
            float: right;
        }
    }

  }
}
</style>
